package com.sgb.kjwl.view.ui.fragment.self;

import android.app.AlertDialog;

import androidx.databinding.DataBindingUtil;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentMyPriceListBinding;
import com.sgb.kjwl.di.business.BusinessFragModule;
import com.sgb.kjwl.di.business.DaggerBusinessFragComponent;
import com.sgb.kjwl.model.entity.user.PublicMyPriceListEntity;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.LeaseQuedPriceEvent;

import com.sgb.kjwl.view.ui.activity.leaseAsk.AskLeaseQuedPriceActivity;

import com.sgb.kjwl.view.ui.adapter.self.publish.PublicMyPriceListAdapter;
import com.sgb.kjwl.viewmodel.machine.PublicMyPriceModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.MToast;
import com.swgk.core.xrecyclerview.XRecyclerView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import javax.inject.Inject;

/**
 * 租赁经营——我的报价
 * 全部", "已报价", "已接受", "已忽略
 */
public class MyPriceListFragment extends AppFragment {

    private FragmentMyPriceListBinding binding;

    @Inject
    PublicMyPriceModel viewModel;
    private PublicMyPriceListAdapter adapter;

    private String isAcceptQuote = "";
//    private String categoryType;//求租类型

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerBusinessFragComponent.builder().appComponent(appComponent).businessFragModule(new BusinessFragModule(this)).build().inject(this);
    }

    //id传递进来
    public static MyPriceListFragment newInstance(String isAcceptQuote) {
        MyPriceListFragment fragment = new MyPriceListFragment();
        Bundle bundle = new Bundle();
        bundle.putString("isAcceptQuote", isAcceptQuote);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_my_price_list, container, false);
        binding.setViewModel(viewModel);
        return binding.getRoot();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        isAcceptQuote = getArguments().getString("isAcceptQuote");
        initView();
        loadData(false);
    }

    private void initView() {
        if (adapter == null) {
            adapter = new PublicMyPriceListAdapter(getActivity(), null,"0");
        }
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.rlvView.setLayoutManager(layoutManager);
        binding.rlvView.setAdapter(adapter);
        binding.rlvView.setLoadingMoreEnabled(true);
        binding.rlvView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                loadData(false);
            }

            @Override
            public void onLoadMore() {
                if (viewModel.hasNext) {
                    loadData(true);
                } else {
                    Log.d("", "没有更多数据");
                    binding.rlvView.loadMoreComplete();
                }
            }
        });

        adapter.setonBtnClick(new PublicMyPriceListAdapter.onBtnClickListener() {
            @Override
            public void onChangePriceClick(PublicMyPriceListEntity entity, int position) {
                //更改报价
                AskLeaseQuedPriceActivity.start(getActivity(), entity.getRentalSeekId(), entity.getCategoryType(), entity.getQuoteUtil(), entity.getNegotiable());
            }

            @Override
            public void onCancelClick(PublicMyPriceListEntity entity, int position) {
                //删除弹窗
                showDelDialog(entity, "删除提示", "您确定要删除当前信息？ 删除后，将不可恢复。");
            }

            @Override
            public void onViewDetailsClick(PublicMyPriceListEntity entity, int position) {
                //查看详情-H5
                viewModel.getH5Url(entity.getRentalSeekId(), entity.getCategoryType());
            }

            @Override
            public void onItemClick(PublicMyPriceListEntity entity, int position) {

            }
        });

    }



    public void loadData(boolean showLoading) {
        viewModel.getDataList(isAcceptQuote, showLoading);
    }

    public void showList(List<PublicMyPriceListEntity> list) {
        binding.rlvView.refreshComplete();
        adapter.setDatas(list);
        if (list.size() == 0) {
            binding.ivEmpty.setVisibility(View.VISIBLE);
        } else {
            binding.ivEmpty.setVisibility(View.GONE);
        }
    }

    public void showMoreList(List<PublicMyPriceListEntity> list) {
        binding.rlvView.loadMoreComplete();
        adapter.addDatas(list);
        binding.ivEmpty.setVisibility(View.GONE);
    }

    //中间显示的Toast
    public void showDelSuccToast(String word) {
        Toast toast = new Toast(getActivity());
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.pop_toast_center, null);
        TextView tvWord = view.findViewById(R.id.tv_word);
        tvWord.setText(word);
        toast.setView(view);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }

    //删除报价提示弹窗
    public void showDelDialog(PublicMyPriceListEntity entity, String title, String content) {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        View layout = inflater.inflate(R.layout.dialog_two_btn_del_hint, null);
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        builder.setView(layout);
        builder.setCancelable(true);
        AlertDialog dialog = builder.show();
        TextView tvTitle = layout.findViewById(R.id.tv_title);
        TextView tvMessage = layout.findViewById(R.id.tv_message);
        TextView tvCancel = layout.findViewById(R.id.tv_cancel);
        TextView tvConfirm = layout.findViewById(R.id.tv_confirm);
        tvTitle.setText(title);
        tvMessage.setText(content);
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                if (!TextUtils.isEmpty(entity.getId())) {
                    viewModel.delPrice(entity.getId());
                } else {
                    MToast.showToast(getActivity(), "报价信息出错无法删除");
                }
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEvent(LeaseQuedPriceEvent event) {
        if (event.getMessageTag() == EventConstant.EVENT_MY_PRICE_LIST) {
            loadData(false);
            EventBus.getDefault().removeStickyEvent(event);
        }
    }
}
